/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-20 10:04:58
 * @filename : static/nmm/src/views/singin.js
 * @version : 
 * @description : 
 */
define(function(require, exports, module){

    var SignInView,mId='signin',reRender=false,
        tpl = require('{view}signin.tpl');

    SignInView = Backbone.View.extend({

        el: 'body',
        template: _.template(tpl),

        initialize: function () {},

        bindEvt : function() {
            var dom = $('#'+mId),tthis = this,
                input  = dom.find('.signup_item').find('input');

            dom.find('.back').tap(function(){
                $('#'+mId).find('.fixedMsg').hide();
                window.location.href='#index/whole';
            });

            dom.find('.btn-signin').tap(function(){
                tthis.handleSigninData();
            });

            dom.find('.forget_pswd').tap(function(){
                tthis.goForgetPassword();
            }); 

            dom.find('.sign_up').tap(function(){
                window.location.href='#joinus/whole';
            });

            dom.find('.facebookbg').tap(function(){
                 window.location.href='#userList/whole'; 
            });

            dom.find('.fixedErr').tap(function(){
                 $(this).parent().hide();
            });

            input.keypress(function(e){
                 if (e.keyCode == 13) {
                     tthis.handleSigninData();
                 } 
            });

        },
 
        render: function (obj) {
            share.checkPermission(false);
            share.reRender(reRender,mId);
            if ($('#'+mId).length < 1) { 
                $('body').append(this.template(lang)); 
                this.bindEvt();
            } else {
                $('#'+mId).css('display','block');
            }

            if (obj.num) {
                $('#'+mId).find('.fixedMsg').show();
                setTimeout(function(){
                   $('#'+mId).find('.fixedMsg').hide();
                },10000);
            }
        },

        goForgetPassword : function() {
            window.location.href='#forgetPassword/whole'; 
        },

        goBack : function() {
            window.history.go(-1);
        },

        handleSigninData : function() {
            $('#'+mId).find('.fixedMsg').hide();
            if (parseInt($('.btn-singin').attr('ajax')) !== 1) {
                var obj = {};
                obj.username = $('#'+mId).find('input[name="userName"]').val();
                obj.password = $('#'+mId).find('input[name="passWord"]').val();
                if (this.singinValidata(obj)) {
                    this.ajaxSingin(obj);
                }
            }
        },

        ajaxSingin : function(data) {
            var tthis = this,dom = $('.btn-signin');
                dom.attr('ajax',1);
                share.ajaxStartA(dom);
            var  obj = {
                url : seajs.data.vars.apiUrl+'login',
                type : 'POST',
                data : data,
                success : function(data) {
                    if (typeof(data.errcode) == 'undefined') {
                        share.setStorage("ms_user_info", JSON.stringify(data));
                        share.setStorage(data.usr_id, JSON.stringify(share.compatibleData(data))); 
                        util.setCookie('session_id',data.session_id,{path:'/'});
                        seajs.data.vars.apiAccessUrl =  domain+'/apis/'+data.session_id+'/';
                        window.location.href='#userList/whole';
                        share.delAllPage();
                    } else {
                        var obj = {};
                        obj.type =3;
                        obj.dom = $('.password');
                        obj.msg = data.errmsg;
                        obj.bool = false;
                        share.inputError(obj);
                    }
                }, 
                error : function(xhr, errorType, error) {
                    share.error('alert','http request failed!'); 
                },
                complete : function(xhr,status){
                    dom.attr('ajax',0);
                    share.ajaxEndA(dom);
                }
            };
            $.ajax(share.ajaxControl(obj));
        },

        singinValidata : function(data) {
            var obj = {bool:true,type:1};
            for (var i in data) {
                if ($.trim(data[i]) == '') {
                    obj.bool = false;
                }
                obj.dom = $('#'+mId).find('.'+i);
                obj.msg = lang.required; 
                share.inputError(obj);
            }
            return obj.bool;
        }
    });

    module.exports = SignInView;
})
